﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Account_Manager : PageBase
{
    protected void Page_Init(object sender, EventArgs e)
    {
        ConfigureUserRoleAccess(new string[] { "Administrator" });
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        DataResponse<List<Role>> rolesResponse = RoleManager.GetAllRoles();

        if (rolesResponse != null)
        {
            if (string.IsNullOrEmpty(rolesResponse.ErrorCode))
            {
                ddlRoles.Roles = rolesResponse.Result;
            }
        }
    }

    protected void btnRegister_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            string username = Convert.ToString(txtUserName.Text).Trim();
            string firstName = Convert.ToString(txtFirstName.Text).Trim();
            string lastName = Convert.ToString(txtLastName.Text).Trim();
            string emailAddress = Convert.ToString(txtEmailAddress.Text).Trim();
            string password = Convert.ToString(txtPassword.Text).Trim();

            User user = new User();
            user.Username = username;
            user.FirstName = firstName;
            user.LastName = lastName;
            user.EmailAddress = emailAddress;
            user.Password = CryptographicHelper.GetMd5Hash(password);

            user.UserRole = new Role();
            user.UserRole.RoleID = Convert.ToInt64(ddlRoles.SelectedItem.Value);

            DataResponse<User> loginResponse = UserManager.CreateUser(user);

            if (loginResponse != null)
            {
                if (string.IsNullOrEmpty(loginResponse.ErrorCode))
                {
                    if (loginResponse.Result.UserID.HasValue)
                    {
                        registerMessage.Visible = true;
                        registerMessage.Attributes.Add("class", "success");
                        registerMessage.InnerHtml = String.Format("User \"{0}\" was created successfully!", user.Username);

                        txtUserName.Text = string.Empty;
                        txtFirstName.Text = string.Empty;
                        txtLastName.Text = string.Empty;
                        txtEmailAddress.Text = string.Empty;
                    }
                    else
                    {
                        registerMessage.Visible = true;
                        registerMessage.Attributes.Add("class", "error");
                    }
                }
                else
                {
                    registerMessage.Visible = true;
                    registerMessage.Attributes.Add("class", "error");
                }
            }
            else
            {
                registerMessage.Visible = true;
                registerMessage.Attributes.Add("class", "error");
            }
        }
        else
        {
            registerMessage.Visible = false;
        }
    }

}
